import {listData} from '@/api/system/dict/data';
import Vue from 'vue'

/**
 * 生成form表单的验证规则
 * @param name  字段名称
 * @param triggerType 触发的方式
 * @returns {{trigger: string, message: string, required: boolean}}
 */
export const createRulesData = (name, trigger = 'blur')=>{
  return {
    required: true,
    message: `${name}不能为空`,
    trigger
  };
}

/**
 *
 *
 * 按parentId来进行分类
 * @param data
 * @param pid
 * @returns {*[]}
 */
export function dealExcPortTree(data, pid = 100, path = '', identification='id') {
  const temp = [];
  for (const [key, item] of data.entries()) {
    if (item.parentId == pid) {
      let itemPath = path ? `${path}.${item.field}` : item.field;

      item.path   = itemPath;
      item.rowKey = item[identification];
      item.children = dealExcPortTree(data, item[identification], itemPath, identification);
      temp.push(item);
    }
  }
  return temp;
}

/**
 * 排序
 * @param data
 * @param sortField 排序的字段
 * @returns {*}
 */
export function excPortTreeSortList(data, sortField='orderNum') {
  data.sort((obj1, obj2) => {
    return obj1[sortField] - obj2[sortField];
  });

  data.map(item=>{
    if(Array.isArray(item.children)) {
      item.children = excPortTreeSortList(item.children);
    }
    return item;
  })

  return data;
}


/**
 *  将险种标准格式转为树形结构
 * @param data 要转化的数据 格式是:Object
 * @param path 路径
 * @returns {*[]}
 */
export function dealRiskTree(data, path = '')  {

  let tree = [];
  for (let item in data) {
    let value = data[item];
    const valueType = Object.prototype.toString.call(value);

    if(valueType == '[Object Object]') {
      let paths = path ? `${path}.${item}` : item;

      let children = dealRiskTree(value, paths);

      tree.push({
        label: item,
        field: item,
        name: '',
        path: paths,
        children: children
      });
    } else if(valueType == '[Object Array]') {

      let chirldrens = [];
      for (let [key, field] of value.entries()) {
        let paths = path ? `${path}.${item}` : item;

        let children = dealRiskTree(field, paths);

        chirldrens.push({
          label: '',
          field: '',
          name: '',
          path: paths,
          children: children
        })
      }

      let paths = path ? `${path}.${item}` : item;

      tree.push({
        label: item,
        field: item,
        name: '',
        path: paths,
        children: chirldrens
      });
    } else {
      tree.push({
        label: `${item}-${value}`,
        name: value,
        field: item,
        path: path ? `${path}.${item}` : item
      });
    }
  }

  return tree;
}


export function copyData(data) {
  return new Promise((resolve, reject)=>{
    let copyInput = document.createElement('input');//创建input元素
    document.body.appendChild(copyInput);//向页面底部追加输入框
    copyInput.setAttribute('value', data);//添加属性，将url赋值给input元素的value属性
    copyInput.select();//选择input元素
    document.execCommand("Copy");//执行复制命令
    //复制之后再删除元素，否则无法成功赋值
    copyInput.remove();//删除动态创建的节点
    resolve();
  })
}

// 字典封装promise
export async function getDictData(dictType) {
  const param = {
    dictType,
    pageNum: 1,
    pageSize: 1000
  }

  const res = await listData(param);

  return new Promise((resolve, reject)=>{
    if(res.code == 200) {
      resolve(res.rows);
    }
  })
}

export function promiseDict(dictField) {
  let dictData = {};
  return new Promise((resolve, reject)=>{
    dictField.forEach(field=>{
      getDictData(field).then(res=>{
        Vue.set(dictData, field, res);
        resolve(dictData);
      });
    })
  });
}


